<?php
/*
 * 视频后台逻辑处理程序
 * @package    auto
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: 7d9edfa823b14b8575a9af5e3dbb86ffb67ff964 $
 */
defined('IN_IXDCW') or exit('Access Denied');
$moduleid = isset($moduleid) ? intval($moduleid) : 0;
$do       = new VideoModule($moduleid);
$BRAND    = array();
$itemid   = isset($itemid) ? $itemid : 0;
$CATEGORY = PCache::read('category-' . $moduleid . '.php');
$menus    = array(
	array('添加' . $MOD['name'], '?moduleid=' . $moduleid . '&action=add'),
	array($MOD['name'] . '列表', '?moduleid=' . $moduleid),
	array('审核' . $MOD['name'], '?moduleid=' . $moduleid . '&action=check'),
	array('未通过' . $MOD['name'], '?moduleid=' . $moduleid . '&action=reject'),
	array('重名检测', '?file=repeat&mid=' . $moduleid . '&key=title&num=100&submit=1'),
	array('回收站', '?moduleid=' . $moduleid . '&action=recycle'),
);

if ($_catids) require DT_ROOT . '/admin/admin_check.inc.php';
if (in_array($action, array('', 'check', 'reject', 'recycle'))) {
	$sfields = array('关键词');
	$dfields = array('title');
	$sorder  = array('添加时间降序', '添加时间升序', '更新时间降序', '更新时间升序', '信息ID降序', '信息ID升序');
	$dorder  = array('addtime DESC', 'addtime ASC', 'edittime DESC', 'edittime ASC', 'id DESC', 'id ASC');
	isset($fields) && isset($dfields[$fields]) or $fields = 0;
	isset($order) && isset($dorder[$order]) or $order = 0;

	$fields_select = dselect($sfields, 'fields', '', $fields);
	$order_select  = dselect($sorder, 'order', '', $order);

	$condition = '';
	if ($_childs) $condition .= " AND catid IN (" . $_childs . ")"; //CATE
	if ($keyword) $condition .= " AND $dfields[$fields] LIKE '%$keyword%'";
	if ($catid) $condition .= ($CATEGORY[$catid]['child']) ? " AND catid IN (" . $CATEGORY[$catid]['arrchildid'] . ")" : " AND catid=$catid";
	if ($level) $condition .= " AND level=$level";
	if ($fromtime) $condition .= " AND `$datetype`>=$fromtime";
	if ($totime) $condition .= " AND `$datetype`<=$totime";
	if ($thumb) $condition .= " AND thumb<>''";
	$timetype = strpos($dorder[$order], 'edit') === false ? 'add' : '';
	if ($psize) {
		$do->setPagesize($psize);
	}
}
switch ($action) {
	case 'add':
		if ($submit) {
			if ($do->pass($post)) {
				$do->add($post);
				dmsg('添加成功', '?moduleid=' . $moduleid . '&action=' . $action);
			} else {
				msg($do->getErrmsg());
			}
		} else {
			foreach ($do->getFields() as $v) {
				isset($$v) or $$v = '';
			}
			$content = '';
			$status  = 3;
			$addtime = timetodate($DT_TIME);
			$item    = array();
			$menuid  = 0;
			$tname   = $menus[$menuid][0];
			isset($url) or $url = '';
			if ($url) {
				$tmp = fetch_url($url);
				if ($tmp) extract($tmp);
			}
			$pagebreak = 0;

			include tpl('edit', $module);
		}
		break;
	case 'edit':
		$itemid or msg();
		$do->setItemid($itemid);
		if ($submit) {
			if ($do->pass($post)) {
				$do->edit($post);
				dmsg('修改成功', '?moduleid=' . $moduleid);
			} else {
				msg($do->getErrmsg());
			}
		} else {
			$item = $do->getOne();
			extract($item);
			$pagebreak = strpos($item['content'], '[pagebreak]') === false ? 0 : 1;
			$addtime   = timetodate($addtime);
			$menuon    = array('4', '3', '2', '1');
			$menuid    = $menuon[$status];
			$tname     = '修改' . $MOD['name'];
			include tpl($action, $module);
		}
		break;
	case 'updateCatid':
		is_array($itemid) or msg('请选择' . $MOD['name']);
		$catid or msg('请选择分类!');
		$do->updateCatid($catid, $itemid);
		dmsg('更新成功', Env::referer());
		break;
	case 'updateAutoCat':
		is_array($itemid) or msg('请选择' . $MOD['name']);
		$arbid or msg('请选择品牌!');
		$do->updateAutoCat($arbid, $arcid, $armid, $itemid);
		dmsg('更新成功', Env::referer());
		break;
	case 'delete':
		$itemid or msg('请选择' . $MOD['name']);
		isset($recycle) ? $do->recycle($itemid) : $do->delete($itemid);
		dmsg('删除成功', $forward);
		break;
	case 'restore':
		$itemid or msg('请选择' . $MOD['name']);
		$do->restore($itemid);
		dmsg('还原成功', $forward);
		break;
	case 'clear':
		$do->clear();
		dmsg('清空成功', $forward);
		break;
	case 'level':
		$itemid or msg('请选择' . $MOD['name']);
		$level = intval($level);
		$do->level($itemid, $level);
		dmsg('级别设置成功', $forward);
		break;
	case 'recycle':
		$lists  = $do->getList('status=0' . $condition, $dorder[$order]);
		$menuid = 4;
		include tpl('index', $module);
		break;
	case 'reject':
		if ($itemid && !$psize) {
			$do->reject($itemid);
			dmsg('拒绝成功', $forward);
		} else {
			$lists  = $do->getList('status=1' . $condition, $dorder[$order]);
			$menuid = 3;
			include tpl('index', $module);
		}
		break;
	case 'check':
		if ($itemid && !$psize) {
			$do->check($itemid);
			dmsg('审核成功', $forward);
		} else {
			$lists  = $do->getList('status=2' . $condition, $dorder[$order]);
			$menuid = 2;
			include tpl('index', $module);
		}
		break;
	default:
		$lists  = $do->getList(' status=3 ' . $condition, $dorder[$order]);
		$menuid = 1;
		include tpl('index', $module);
		break;
}

